package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int count = bookList.getUseSize();
        //判断是否有这本书
        int i = 0;
        int cur = 0;
        for ( i = 0; i < count; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                cur = i;
                break;
            }
        }
        if (i == count){
            System.out.println("未找到"+name+"这本书");
        }
        //删除你想要删除的书
        for (int j =cur ; j < count -1; j++) {
            //前移覆盖
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(cur,book);
        }
        //清除原最后一位的引用
        bookList.setBooks(count -1,null);
        bookList.setUseSize(count -1);
        System.out.println("删除成功");
    }
}
